<template>
	<view class="content">
		<qian-dao :list="list" :date="date" :time_key="time_key"
			@date_change="date_change_fun"></qian-dao>
			<view class="DataSearch">
				<view>已签到{{LxDate}}天</view>
				<view>已连续签到
				<span v-if="qdDtae == null">0</span>
				<span v-else>{{qdDtae}}</span>
				天</view>
			</view>
		<u-button text="签到" @click="day_change_fun" size="normal" :color="ButtonShow === true ? 'linear-gradient(rgb(220, 194, 11))':''" shape="circle" color="linear-gradient(to right, rgb(220, 194, 11), rgb(4, 151, 99))">
		</u-button>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				// 当前的日期
				date: "",
				// 存放时间的key值
				time_key: "",
				// 已经签到的数据列表
				list: [],
				ButtonShow:false,
				LxDate:'',
				qdDtae:''
			}
		},
		methods: {
			SearchSign() {
				this.$http({
					url: '/SignIn/getSignInfo',
					method: 'POST'
				}).then(res => {
					for(let key in res.data.data){
						if(res.data.data[key] == true){
							this.list.push(key)
						}
					}
					this.SearchDate()
				})
			},
			day_change_fun(day) {
				// console.log(JSON.parse(JSON.stringify(day)));
				this.$http({
					url:'/SignIn/doSign',
				}).then(res=>{
					if(res.data.code == 200){
						uni.showToast({
							title:'签到成功'
						})
					}else{
						uni.showToast({
							title:res.data.message,
							icon:'error'
						})
					}
				})
				// 存放时间的key值
				// let time_key = this.time_key;

				// 如果没有签到（可以补签，需要补签的可以这么写）
				// if (!day.click) {
				// 	if (time_key) {
				// 		let obj = {};
				// 		obj[time_key] = day.nyr;
				// 		this.list.push(obj);
				// 	} else {
				//  	this.list.push(day.nyr);
				// 	}
				// }
				// 如果今天没有签到(只签到今天的，如果只需要签到今天的可以这么写)
				// if (!day.click && day.type == "today") {
				//  if (time_key) {
				//      let obj = {};
				//      obj[time_key] = day.nyr;
				//      this.list.push(obj);
				//  } else {
				//      this.list.push(day.nyr);
				//  }
				// }
			},
			// 日期改变时触发
			date_change_fun(date) {
				// 更新日期
				this.date = date;

				// 清空已经签到的列表
				this.list = [];

				// 根据日期获取已经签到的列表然后赋值
				// 存放时间的key值
				let time_key = this.time_key;
				if (time_key) {
					this.list.push({
						time: "01"
					});
				} else {
					this.list.push("01");
				}
				this.SearchDate()
				this.SearchSign()
				this.LxData()
				this.QdData()
			},
			SearchDate(){
				this.$http({
					url:'/SignIn/checkSign',
					method:'POST'
				}).then(res=>{
					this.ButtonShow = res.data.data
					this.LxData()
				})
			},
			LxData(){//签到天数
				this.$http({
					url:'/SignIn/getSignCount',
					method:'POST'
				}).then(res=>{
					this.LxDate = res.data.data
					this.QdData()
				})
			},
			QdData(){//连续签到天数
				this.$http({
					url:'/SignIn/getContinuousSignCount',
					method:'POST'
				}).then(res=>{
					this.qdDtae = res.data.data
				})
			}
		},
		onShow(){
			this.SearchSign()
			setTimeout(()=>{
				this.SearchSign()
			},2000)
		}
	}
</script>

<style scoped>
  @import './sign.css';
</style>
